from skimage.measure import compare_ssim
import argparse
import imutils
import cv2
import time
import numpy as np

#调用笔记本内置摄像头，所以参数为0，如果有其他的摄像头可以调整参数为1，2
cap=cv2.VideoCapture(0)

#从摄像头读取图片
sucess,InitializeSSIMA=cap.read()
gray=cv2.cvtColor(InitializeSSIMA,cv2.COLOR_BGR2GRAY)
cv2.imwrite("InitializeSSIMA.png",InitializeSSIMA)
imageA = cv2.bilateralFilter(InitializeSSIMA,9,75,75)
grayA = cv2.cvtColor(InitializeSSIMA, cv2.COLOR_BGR2GRAY)

SSIM_All = []

for i in range(5):
    i=0
    i=i+1
    sucess,InitializeSSIMB=cap.read()
    time.sleep(0.5)
    cv2.imwrite("InitializeSSIMB.png",InitializeSSIMB)
    grayB = cv2.cvtColor(InitializeSSIMB, cv2.COLOR_BGR2GRAY)
    imageB = cv2.bilateralFilter(InitializeSSIMB,9,75,75)
    #计算两者之间的结构相似性指数
    #图像，确保返回差异图像
    (score, diff) = compare_ssim(grayA, grayB, full=True)
    diff = (diff * 255).astype("uint8")
    print("ssim: {}".format(score))
    
    SSIM_All.append(score)
    
print("ALL_SSIM: ", SSIM_All)

SSIM_AVR = (float(SSIM_All[0])+float(SSIM_All[1])+\
            float(SSIM_All[2])+float(SSIM_All[3])+\
            float(SSIM_All[4]))/5

print(SSIM_AVR)
cv2.destroyAllWindows()


def InitializeSSIM():
    #调用笔记本内置摄像头，所以参数为0，如果有其他的摄像头可以调整参数为1，2
    cap=cv2.VideoCapture(0)

    #从摄像头读取图片
    sucess,InitializeSSIMA=cap.read()
    gray=cv2.cvtColor(InitializeSSIMA,cv2.COLOR_BGR2GRAY)
    cv2.imwrite("InitializeSSIMA.png",InitializeSSIMA)
    imageA = cv2.bilateralFilter(InitializeSSIMA,9,75,75)
    grayA = cv2.cvtColor(InitializeSSIMA, cv2.COLOR_BGR2GRAY)

    SSIM_All = []

    for i in range(5):
        i=0
        i=i+1
        sucess,InitializeSSIMB=cap.read()
        time.sleep(0.5)
        cv2.imwrite("InitializeSSIMB.png",InitializeSSIMB)
        grayB = cv2.cvtColor(InitializeSSIMB, cv2.COLOR_BGR2GRAY)
        imageB = cv2.bilateralFilter(InitializeSSIMB,9,75,75)
        #计算两者之间的结构相似性指数
        #图像，确保返回差异图像
        (score, diff) = compare_ssim(grayA, grayB, full=True)
        diff = (diff * 255).astype("uint8")
        print("ssim: {}".format(score))
        
        SSIM_All.append(score)
        
    print("ALL_SSIM: ", SSIM_All)

    SSIM_AVR = ((float(SSIM_All[0])+float(SSIM_All[1])+\
                float(SSIM_All[2])+float(SSIM_All[3])+\
                float(SSIM_All[4]))/5)-0.01

    print(SSIM_AVR)
    cv2.destroyAllWindows()

def A():
    x=(1*3)
    return x

def B():
    A()
    print(A())
    x=A()
    y=x+1
    print(y)
B()













    
    
